/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.webserver.handler;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mb.config.Constants;
import mb.mbmiddle.MbPartMiddle;
import mb.mbpart.memory.BaseMemory;
import mb.webserver.inMemory.ATMDataProcess;
import mb.webserver.inMemory.MBServerConfigProcess;

/**
 *
 * @author thanh.nguyenchi
 */
public class ATMData extends BaseServlet{   
    
    public ATMData(){
        super();        
    }
    
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {       
        
        ///api/loadATMData?version={1}&lang={2}&cachetime={3}&callback={4}
        String sOut = "";       
        String version          = request.getParameter("version");              // check 
        String lang             = request.getParameter("lang");                 // log
        String cachetime        = request.getParameter("cachetime");            // log
        String callback         = request.getParameter("callback");             // check 
        
        StringBuffer sbLog = new StringBuffer();
        sbLog.append("ATMData ==> [v:" + version);
        sbLog.append("] [l:" + lang + "] c:[" + cachetime);        
        sbLog.append("] [c:" + callback + "] from [ip:" + request.getRemoteAddr() + "]");
        log(sbLog.toString());
        
        // call bussiness
        ATMDataProcess objProcess = new ATMDataProcess(lang,cachetime);
        if ((callback != null) && (callback.length() > 1)){
            sOut = callback + "(" + objProcess.getResult() + ")";
        }
        else{
            // return no callback
            sOut = objProcess.getResult();
        }
        
        response.setContentType("application/json; charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(sOut);
    }    
    
}
